﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SK_Relations;
using QRCoder;
using System.Threading;

namespace MooseForm
{
    public partial class Form1 : Form
    { 
        public Form1()
        {
            InitializeComponent();


            this.Load += Form1_Load;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RegistrationHelper.ProductName = "NA1000S";
            string _Code = RegistrationHelper.Instance.GetRegisterCode();
            label1.Text = _Code.Substring(0, 5) + "-" + _Code.Substring(5, 5) + "-" + _Code.Substring(10, 5) + "-" + _Code.Substring(15, 5) + "-" + _Code.Substring(20, 5);

            pictureBox1.Image = Methods.GetQrCoder($"NA1000S,{label1.Text.Replace("-","")}");
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (RegistrationHelper.Instance.CheckRegisterCode(textBox1.Text))
            {
                MessageBox.Show("成功");
            }
            else
                MessageBox.Show("错误");
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            Thread _Thread = new Thread(new ThreadStart(()=>
            {
                int i = 0;
                while (true)
                {
                    if (label1.InvokeRequired)
                    {
                        label1.Invoke(new Action(()=> 
                        {
                            label1.Text = 20 - i + "";
                        }));
                    }else
                        label1.Text = 20 - i + "";

                    Thread.Sleep(1000);
                    i++;
                    if (i == 20)
                        break;
                }

                try
                {
                    System.Diagnostics.Process CurrPorcess = System.Diagnostics.Process.GetCurrentProcess();
                    CurrPorcess.Kill();
                }
                catch { }
                finally
                {
                    GC.Collect();
                }

            }));
            _Thread.IsBackground = true;
            _Thread.Start();

        }
    }
}
